package cn.zwx.goods.svc.service.impl;

import cn.zwx.goods.svc.domain.CategoryAttrDO;
import cn.zwx.goods.svc.domain.entity.CategoryAttrEntity;
import cn.zwx.goods.svc.domain.entity.CategoryEntity;
import cn.zwx.goods.svc.domain.enums.DelFlag;
import cn.zwx.goods.svc.mapper.CategoryAttrMapper;
import cn.zwx.goods.svc.mapper.CategoryMapper;
import cn.zwx.goods.svc.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zhangwenxue
 */
@Service
public class CategoryServiceImpl  implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryAttrMapper categoryAttrMapper;

    @Override
    public List<CategoryEntity> selectList(LambdaQueryWrapper<CategoryEntity> categoryWrapper) {
        return categoryMapper.selectList(categoryWrapper);
    }

    @Override
    public CategoryEntity selectById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void insert(CategoryEntity categoryEntity) {
        categoryMapper.insertSelective(categoryEntity);
    }

    @Override
    public void update(CategoryEntity categoryEntity) {
        categoryMapper.updateByPrimaryKeySelective(categoryEntity);
    }

    @Override
    public Integer selectCount(LambdaQueryWrapper<CategoryEntity> categoryWrapper) {
        return categoryMapper.selectCount(categoryWrapper).intValue();
    }

    @Override
    public void delete(Integer id) {
        final CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setUpdateTime(new Date());
        categoryEntity.setDelFlag(DelFlag.delete.getCode());
        categoryEntity.setId(id);
        categoryMapper.updateByPrimaryKeySelective(categoryEntity);
    }

    @Override
    public void batchInsertAttr(List<CategoryAttrEntity> categoryAttrList) {
        categoryAttrMapper.batchInsert(categoryAttrList);
    }

    @Override
    public List<CategoryAttrDO> selectAttrList(Integer categoryId) {
        return categoryAttrMapper.selectAttrListByCategoryId(categoryId);
    }

    @Override
    public void deleteAttr(Integer categoryId) {
        LambdaQueryWrapper<CategoryAttrEntity> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.eq(CategoryAttrEntity::getCategoryId,categoryId);
        categoryAttrMapper.delete(categoryWrapper);
    }

    @Override
    public void deleteAttrByAttrId(Long attrId) {
        LambdaQueryWrapper<CategoryAttrEntity> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.eq(CategoryAttrEntity::getAttrId,attrId);
        categoryAttrMapper.delete(categoryWrapper);
    }

    @Override
    public List<CategoryEntity> selectByIdList(List<Integer> categoryIdList) {
        return categoryMapper.selectBatchIds(categoryIdList);
    }
}
